package org.oexam.ostudy.serv;

import java.io.IOException;
import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.oexam.ostudy.facade.TestSetPages;
import org.oexam.ostudy.facade.struct.InsertNewTestDetails;
import org.oexam.ostudy.facade.struct.LoggedUserDetails;
import org.oexam.ostudy.struct.Tests;

/**
 * Servlet implementation class ManageTestSets
 */
@WebServlet("/ViewAndAddTests")
public class ViewAndAddTests extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewAndAddTests() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		LoggedUserDetails details = (LoggedUserDetails) request.getSession()
				.getAttribute("userLoginDetails");

		if (details == null) {
			response.sendRedirect("Login.jsp");
		}
		if (details.isAuthenticated() && (details.getUserType() == 0)) {

			TestSetPages testSetPages = new TestSetPages();
			String action = request.getParameter("action");
			
			if (action == null) {
				response.sendRedirect("ViewAllTestSets.jsp");

			} else {
				if (action.equals("deleteTestSet")) {
					try {
						request.getSession().setAttribute("message", testSetPages.deleteTestSet(Integer.parseInt(request.getParameter("TSID"))));
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (NamingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
					response.sendRedirect("ViewAllTestSets");

				} else if (action.equals("deleteTest")) {
					
					try {
						request.getSession().setAttribute("message", testSetPages.deleteTest(Integer.parseInt(request.getParameter("TID"))));
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (NamingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					response.sendRedirect("AddTestsToTestSet.jsp?TSID="
							+ request.getParameter("TSID"));
				}

				else if (action.equals("AddTestsToTestSet")) {
					response.sendRedirect("AddTestsToTestSet.jsp?TSID="
							+ request.getParameter("TSID"));
				}

			}

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		LoggedUserDetails details = (LoggedUserDetails) request.getSession()
				.getAttribute("userLoginDetails");

		if (details == null) {
			response.sendRedirect("Login.jsp");
		}
		if (details.isAuthenticated() && (details.getUserType() == 0)) {

			TestSetPages testSetPages = new TestSetPages();
			
			InsertNewTestDetails insertNewTestDetails = new InsertNewTestDetails();
			insertNewTestDetails.setActive(Boolean.getBoolean(request.getParameter("isActive")));
			insertNewTestDetails.setNo_of_questions(Integer.parseInt(request.getParameter("noOfQues")));
			insertNewTestDetails.setTeacher(Integer.parseInt(request.getParameter("teacher")));
			insertNewTestDetails.setTest_day(Integer.parseInt(request.getParameter("day")));
			insertNewTestDetails.setTest_month(Integer.parseInt(request.getParameter("month")));
			insertNewTestDetails.setTest_year(Integer.parseInt(request.getParameter("year")));
			insertNewTestDetails.setTest_duration(Integer.parseInt(request.getParameter("testDur")));
			insertNewTestDetails.setTest_name(request.getParameter("testName"));
			insertNewTestDetails.setTest_set(Integer.parseInt(request.getParameter("TSID")));
			
			try {
				request.getSession().setAttribute("message", testSetPages.addGivenTestToTestSet(insertNewTestDetails));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			response.sendRedirect("AddTestsToTestSet.jsp?TSID="
					+ request.getParameter("TSID"));
			
			
		}
	}

}
